/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/

/**
 * @brief Implementation of class attributes for syntactic structure.
 *
 * @author  WXQ#XDU
 * @date    2025.01
 */

#include <stdlib.h>

#include "_emit_inner.h"


// callback in codeattr_destroy()
static void codeattr_clean__(t_reference_ptr attrBasic){
    if (NULL == attrBasic) return;

/* All chains are stored in a memory pool(see backpatch.c),
   so DON'T destroy them at here.


    t_codeattr_ptr attr = (t_codeattr_ptr)attrBasic;
    if (NULL != attr->trueChain) { list_destroy(attr->trueChain);  }
    if (NULL != attr->falseChain){ list_destroy(attr->falseChain); }
    if (NULL != attr->nextChain) { list_destroy(attr->nextChain);  }
*/

}


t_codeattr_ptr codeattr_new() {
    t_codeattr_ptr attr = (t_codeattr_ptr) calloc (1, sizeof (*attr));
    ref_init_((t_reference_ptr) attr, codeattr_clean__);
    return attr;
}


void codeattr_destroy(void* attrPtr) {
//    log_trace("XXX  free annotation %tx, counter=%d", attrPtr, ((t_codeattr_ptr)attrPtr)->base.refCount);

    // codeattr_clean__() will be called if the ref-counter is 0
    ref_free_((t_reference_ptr)attrPtr);
}


t_codeattr_ptr codeattr_getReference(t_codeattr_ptr attrPtr) {
    attrPtr = (t_codeattr_ptr)ref_copy_((t_reference_ptr)attrPtr);
    return attrPtr;
}


t_codeattr_ptr codeattr_copy(t_codeattr_ptr src) {
    t_codeattr_ptr copy = codeattr_new();
    copy->place        = src->place;
    copy->offset       = src->offset;
    copy->type         = src->type;
    copy->trueChain    = src->trueChain;
    copy->falseChain   = src->falseChain;
    copy->nextChain    = src->nextChain;
    copy->firstCodeId  = src->firstCodeId;
    return copy;
}
